package com.vueapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vueapi.constant.ErrorCodeEnum;
import com.vueapi.domain.HjOperationLog;
import com.vueapi.domain.HjUser;
import com.vueapi.domain.dto.PasswordDTO;
import com.vueapi.domain.vo.UserInfoVo;
import com.vueapi.exception.BlogException;
import com.vueapi.exception.UserException;
import com.vueapi.service.HjOperationLogService;
import com.vueapi.service.HjUserService;
import com.vueapi.mapper.HjUserMapper;
import com.vueapi.utils.HttpContextUtil;
import com.vueapi.utils.IPUtils;
import com.vueapi.utils.JwtUtil;
import com.vueapi.utils.ResultUtils;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
* @author 15442
* @description 针对表【hj_user】的数据库操作Service实现
* @createDate 2022-09-20 11:44:51
*/
@Service
public class HjUserServiceImpl extends ServiceImpl<HjUserMapper, HjUser>
    implements HjUserService{

    @Override
    public UserInfoVo getUserInfoByToken(String token) {
        String usernameFromToken = JwtUtil.getUsernameFromToken(token);
        HjUser user = this.getOne(new LambdaQueryWrapper<HjUser>().eq(HjUser::getUsername, usernameFromToken));
        if(user == null){
            return null;
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user,userInfoVo);
        return userInfoVo;
    }

    @Override
    public String login(HjUser user) {
        // 获取请求参数
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();

        long start = System.currentTimeMillis();
        String username = user.getUsername();
        String password = user.getPassword();
        String md5Pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        // 需要用MD5加密
        HjUser one = this.getOne(new LambdaQueryWrapper<HjUser>().eq(HjUser::getUsername, username).eq(HjUser::getPassword, md5Pwd));
        if(one == null){
            return null;
        }

        return JwtUtil.generateToken(user);
    }

    @Override
    public ResultUtils editPassword(PasswordDTO passwordDTO) {
        String password = passwordDTO.getPassword();
        String confirmPassword = passwordDTO.getConfirmPassword();
        if(passwordDTO.getUsername().isEmpty() || password.isEmpty() || confirmPassword.isEmpty()){
            return ResultUtils.fail("用户或密码不能未空!");
        }

        if(!confirmPassword.equals(password)){
            return ResultUtils.fail("两次密码不一致!");
        }

        HjUser user = this.getById(passwordDTO.getId());
        if(user == null){
            return ResultUtils.fail("此用户不存在!");
        }

        String md5Pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        HjUser hjUser = new HjUser();
        hjUser.setId(passwordDTO.getId());
        hjUser.setPassword(md5Pwd);
        if(this.updateById(hjUser)){
            return ResultUtils.success();
        }
        return ResultUtils.fail("修改失败!");

    }
}




